Conversation
|
|
||
| l2 = l2->next; | ||
| } | ||
| if (carryUp == true) { |
There was a problem hiding this comment.
if (carryUp) {で十分伝わると思います。
偽であることを確認する際、 ! 演算子だと読みにくいため、 if (false == carryUp) と書くようにしているという方を見たことがありますが、少数派のように思います。
There was a problem hiding this comment.
boolであればご提案の形で十分ですね。ありがとうございます。
intで扱っている場合には可読性の面から注意が必要かもしれないと感じました。
| node->next = new ListNode(sum % 10); | ||
| node = node->next; | ||
|
|
||
| carryUp = sum/10; |
There was a problem hiding this comment.
演算子の両側にスペースを空けるか空けないか、統一されることをお勧めいたします。
There was a problem hiding this comment.
空ける方針で統一しようと思います。ありがとうございます。
xbam326
left a comment
There was a problem hiding this comment.
所用時間や知っていたことと調べたことが分けて書いてあるのが良いなと思いました。
| ## Step3 リファクタリング | ||
| - 所要時間: 10min | ||
| - 方針: | ||
| - boolではなくintにしてif文を減らす |
There was a problem hiding this comment.
こちらif文を減らす以外に、3つ以上の数字を足すなどの拡張を考えた際に繰り上げが0or1でなくなるのでboolよりもintの方が良さそうだと私は判断しました。
There was a problem hiding this comment.
その観点は確かになかったです。ご指摘ありがとうございます。
boolは内部的には0か非ゼロでしかないので、3以上の状態をとるときはint、2つの状態しかとらないときは可読性を考慮して都度選択、がいいと思いました。今回はintでよかったですね。
|
|
||
| while (l1 != nullptr || l2 != nullptr || carryUp == true) { | ||
| const int x = (l1 != nullptr) ? l1->val : 0; | ||
| const int y = (l2 != nullptr) ? l2->val : 0; |
There was a problem hiding this comment.
pythonにはない記法ですよね。
シンプルになるけど、可読性という意味で、競プロ経験者やつよつよさん以外には読みにくいと思うので考えどころです。
| - constへの代入、単純なA/B選択などで可読性を落とさず使える | ||
| - boolのメリットデメリット | ||
| - メリット: 可読性が高い | ||
| - デメリット: 処理のためにif文を介さないといけないため実行速度が落ちる |
There was a problem hiding this comment.
確かに!!!ありがとうございます
実際最適化の段階で何がどうなっているのか、雰囲気でしかわかっていないので調べてみます。
|
「Arai60/2」というタイトルに Add Two Numbers という問題名を足しておくと Discord 内で検索されやすくなって他の人が助かるだろうと思います。 |
2. Add Two Numbers